﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Cms.Tests.Unit.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class MenuTests
    {

        [TestMethod]
        public void CloneMenu_Success()
        {
            Menu original = new Menu
                                {
                                    ID = 1,
                                    ItemIsActiveWhenSubItemIsActive = true,
                                    Name = "Top menu",
                                    Site = new Site {ID = 1},
                                    StyleSheet = new SourceCode {ID = 1},
                                    SubMenuBehaviour = SubMenuBehaviour.OpenExclusively,
                                    ActiveItemPrefix = "*", 
                                    Description = "The menu in the header"
                                };
            original.Versions.Add(MenuTestsHelper.CreateVersion());
            original.PublishedVersion = original.Versions[0];
            Menu clone = (Menu)original.Clone();
            Assert.AreEqual(0, clone.ID);
            Assert.AreEqual("Top menu", clone.Name);
            Assert.AreSame(original.Site, clone.Site);
            Assert.IsTrue(clone.ItemIsActiveWhenSubItemIsActive);
            Assert.AreNotSame(original.StyleSheet, clone.StyleSheet);
            Assert.IsNotNull(clone.StyleSheet);
            Assert.AreEqual(SubMenuBehaviour.OpenExclusively, clone.SubMenuBehaviour);
            Assert.IsNotNull(clone.PublishedVersion);
            Assert.AreNotSame(original.PublishedVersion, clone.PublishedVersion);
            Assert.AreSame(clone, clone.PublishedVersion.Parent);
            Assert.AreEqual(1, clone.Versions.Count);
            Assert.AreSame(clone.PublishedVersion, clone.Versions[0]);
            Assert.AreEqual("*", clone.ActiveItemPrefix);
            Assert.AreEqual("The menu in the header", clone.Description);
        }


        [TestMethod]
        public void GetStylesheetCode_NewlyCreatedMenu_Empty()
        {
            Menu menu = new Menu();
            Assert.AreEqual("", menu.GetStyleSheetCode());
        }


        [TestMethod]
        public void GetStylesheetCode_EditExistingMenu_NotEmpty()
        {
            Menu menu = new Menu();
            menu.SetStyleSheetCode("body { color: #3e3e3e; }", new Administrator());
            Assert.AreEqual("body { color: #3e3e3e; }", menu.GetStyleSheetCode());
        }


        [TestMethod]
        public void SetStylesheetCode()
        {
            Menu menu = new Menu();
            Site site = new Site {ID = 1};
            Administrator admin = new Administrator();
            menu.Site = site;
            menu.SetStyleSheetCode("body { color: #3e3e3e; }", admin);
            menu.SetStyleSheetCode("body { color: #000000; }", new Administrator());
            Assert.AreEqual("body { color: #000000; }", menu.StyleSheet.CurrentVersion.Code);
            Assert.AreEqual(2, menu.StyleSheet.Versions.Count);
            Assert.AreSame(site, menu.StyleSheet.Site);
        }


        [TestMethod]
        public void GetVersion_VersionExists_Success()
        {
            Menu menu = new Menu();
            MenuVersion v = MenuTestsHelper.CreateVersion();
            menu.Versions.Add(v);
            Assert.AreSame(v, menu.GetVersion(1));
        }


        [TestMethod]
        public void GetVersion_VersionDoesNotExists_Null()
        {
            Menu menu = new Menu();
            MenuVersion v = MenuTestsHelper.CreateVersion();
            menu.Versions.Add(v);
            Assert.IsNull(menu.GetVersion(666));
        }


        [TestMethod]
        public void RemoveImage_SeveralItemsContainReferencesToMediaFile_Success()
        {
            Menu menu = new Menu();
            MenuVersion v = MenuTestsHelper.CreateVersion();
            menu.Versions.Add(v);
            MediaFile defaultImage = new MediaFile { ID = 1 };
            MediaFile defaultHoverImage = new MediaFile { ID = 2 };
            MediaFile activeImage = new MediaFile { ID = 3 };
            MediaFile activeHoverImage = new MediaFile { ID = 4 };
            v.Items[0].Translations[1].DefaultImage = defaultImage;
            v.Items[2].Translations[1].DefaultImage = defaultImage;
            menu.RemoveImage(defaultImage);
            Assert.IsNull(v.Items[0].Translations[1].DefaultImage);
            Assert.IsNull(v.Items[2].Translations[1].DefaultImage);

            v.Items[0].Translations[1].DefaultHoverImage = defaultHoverImage;
            menu.RemoveImage(defaultHoverImage);
            Assert.IsNull(v.Items[0].Translations[1].DefaultHoverImage);

            v.Items[0].Translations[1].ActiveImage = activeImage;
            menu.RemoveImage(activeImage);
            Assert.IsNull(v.Items[0].Translations[1].ActiveImage);

            v.Items[0].Translations[1].ActiveHoverImage = activeHoverImage;
            menu.RemoveImage(activeHoverImage);
            Assert.IsNull(v.Items[0].Translations[1].ActiveHoverImage);
        }

    }

}
